<?php


namespace App\Services;


use App\Models\PracticalTraining;
use App\Models\PracticalTrainingUser;
use App\Models\PracticalTrainingEvaluation;
use Illuminate\Support\Facades\DB;

class PracticalTrainingUserService extends BaseService
{
    /**
     * 获取用户实训列表
     * @return
     */
    public function userPracticalList($page,$limit,$sort,$order, $name,$users_id,$practical_training_id) {
        $query = PracticalTrainingUser::leftJoin('practical_training_evaluations as e', function ($join) {
                $join->on('practical_training_users.id', '=', 'e.practical_training_user_id')
                    ->where('e.is_finally', '=', 1);
            })
            ->leftJoin('users', 'users.id', '=', 'practical_training_users.users_id')
            ->leftJoin('users as evaluation_users', 'evaluation_users.id', '=', 'e.users_id')
            ->leftJoin('schools', 'schools.id', '=', 'users.school_id')
            ->select('practical_training_users.*',
                'e.score',
                'e.content as evaluation_content',
                'e.audit',
                'e.audit_time',
                'e.created_at as evaluation_createTime',
                'users.name as practical_name',
                'evaluation_users.name as evaluation_users_name',
                'schools.name as schools_name'
            );
        if(!empty($name)){
            $query->where('practical_training_users.name','like',"%$name%");
        }
        if(isset($users_id)){
            $query->where('practical_training_users.users_id',$users_id);
        }
        if(isset($practical_training_id)){
            $query->where('practical_training_id',$practical_training_id);
        }
        return $query->distinct()->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取用户实训审核列表
     * @return
     */
    public function userPracticalEvaluationsList($page,$limit,$sort,$order,$practical_training_user_id) {
        $query = PracticalTrainingEvaluation::leftJoin('users', 'users.id', '=', 'practical_training_evaluations.users_id')
            ->leftJoin('schools', 'schools.id', '=', 'users.school_id')
            ->select('practical_training_evaluations.*',
                'users.name as users_name',
                'schools.name as schools_name'
            );
        if(isset($practical_training_user_id)){
            $query->where('practical_training_user_id',$practical_training_user_id);
        }
        return $query->distinct()->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取用户实训信息
     * @return
     */
    public function getByPracticalTrainingUserInfo($id) {
        $query = PracticalTrainingUser::leftJoin('practical_training_evaluations', 'practical_training_evaluations.practical_training_user_id', '=', 'practical_training_users.id')
            ->leftJoin('users', 'users.id', '=', 'practical_training_users.users_id')
            ->leftJoin('users as evaluation_users', 'evaluation_users.id', '=', 'practical_training_evaluations.users_id')
            ->leftJoin('schools', 'schools.id', '=', 'users.school_id')
            ->select('practical_training_users.*',
                'practical_training_evaluations.score',
                'practical_training_evaluations.content as evaluation_content',
                'practical_training_evaluations.created_at as evaluation_createTime',
                'users.name as practical_user_name',
                'evaluation_users.name as evaluation_user_name',
                'schools.name as schools_name'
            );
        return $query->where('practical_training_users.id',$id)->get();
    }
}
